package com.guqj.basis.innerclass;

import org.junit.Test;


/**
 * 编译后以下代码会生成两个类，一个是ChengyuanOuter, 一个ChengyuanOuter$Inner
 * public class Outer {
 *   private static int staticShared = 100;
 *   private int shared = 100;
 *   
 *   private void action() {
        System.out.println("action");
    }
    
    public void test() {
        Outer$Inner inner = new Outer$Inner(this);
        inner.innerMethod();
    }
    
    static int access$0(ChengyuanOuter outer) {
        return outer.staticShared;
    }
    
    static int access$1(){
        return outer.shared;
    }
    
    static void access$2(Outer outer) {
        outer.action();
    }
 * }
 * 
 * public class ChengyuanOuter$Inner {
 *   final Outer outer;
 *   
 *   public ChengyuanOuter$Inner(ChengyuanOuter outer){
 *       this.outer=outer;
 *   }
 * 
 *   public void innerMethod() {
 *      System.out.println("static inner " + ChengyuanOuter.access$0());
 *      System.out.println("static inner " + ChengyuanOuter.access$1());
 *      ChengyuanOuter.access$2();
 *   }
 * }
 * 
 * @author guqj
 * @date   2017年3月30日
 */
public class ChengyuanOuter {

	private static int staticShared = 100;
	private int shared = 100;

	/**
	 * <pre>
	 * 	 成员内部类没有static修饰符,成员内部类中不可以定义静态变量和方法(final变量例外，它等同于常量）
	 *   
	 * </pre>
	 */
	public class Inner {
		public void innerMethod() {
			System.out.println("static inner " + staticShared); // 可访问外部类的静态变量和方法
			System.out.println("inner " + shared); // 成员内部类还可以直接访问外部类的实例变量和方法
			ChengyuanOuter.this.action();// 这种写法一般在重名的情况下使用，没有重名的话，"外部类.this."是多余的
			action2();
		}
		
	}
	
	private void action(){
		System.out.println("action");
	}
	
	public void action2(){
		System.out.println("action2");
	}
	
	@Test
	public void test(){
		//ChengyuanOuter outer = new ChengyuanOuter(); // 不能直接通过new ChengyuanOuter.Inner()
		//ChengyuanOuter.Inner inner = outer.new Inner();
		//inner.innerMethod();
		
		Inner inner = new Inner();
		inner.innerMethod();
	}
}
